//
//  LiveViewController.m
//  LiveSoftware
//
//  Created by lanou3g on 15/7/10.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "LiveViewController.h"
#import "CommonView.h"
#import "NetWorkStateObserver.h"


#define kTitleY kScreenIp6(20)
#define kBtnWidth kScreenIp6(60)
#define kBtnHeight kScreenIp6(40)
#define kLiveUrl @"http://m.zhibo8.cc/json/zhibo/saishi.json?aabbccddeeff="

@interface LiveViewController () <UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>

@property(nonatomic,strong) CommonTitleView *liveView;
@property(nonatomic,strong) NSMutableArray *liveModelArray;
@property(nonatomic,copy) NSString *currentTime;

@property(nonatomic,strong) LiveModel *model;
@property(nonatomic,strong) ListArrayModel *smallModel;
@property(nonatomic,strong) ListArrayModel *importantSmallModel;

@property(nonatomic,strong) NSMutableArray *listArray;
// 判定篮球是否被选中
@property(nonatomic,getter=isSelected) BOOL selected;

// 装载关注比赛model的数组
@property(nonatomic,strong) NSMutableArray *footballFocusArray;
@property(nonatomic,strong) NSMutableArray *basketballFocusArray;
@property(nonatomic,copy) NSIndexPath *focusIndexPath;

//@property(nonatomic,strong) NSMutableArray *attentionMatch;

@end

@implementation LiveViewController


- (ListArrayModel *)smallModel
{
    if (!_smallModel) {
        _smallModel = [[ListArrayModel alloc] init];
    }
    return _smallModel;
}
- (ListArrayModel *)importantSmallModel
{
    if (!_importantSmallModel) {
        _importantSmallModel = [[ListArrayModel alloc] init];
    }
    return _importantSmallModel;
}

- (NSMutableArray *)footballFocusArray
{
    if (!_footballFocusArray) {
        _footballFocusArray = [NSMutableArray array];
    }
    return _footballFocusArray;
}

- (NSMutableArray *)basketballFocusArray
{
    if (!_basketballFocusArray) {
        _basketballFocusArray = [NSMutableArray array];
    }
    return _basketballFocusArray;
}

#pragma mark - 解析数据
-(NSString*)getTime
{
    //获取时间
    NSDate *today = [NSDate date];
    
    double dureen = [today timeIntervalSince1970];
    
    NSString *timeString = [NSString stringWithFormat:@"%f",dureen];
    
    NSString* miao = [[timeString componentsSeparatedByString:@"."]objectAtIndex:0];
    NSString *haomiao = [[timeString componentsSeparatedByString:@"."]objectAtIndex:1];
    
//    NSLog(@"---miao--%@",miao);
//    NSLog(@"--haomiao---%@",haomiao);
    
    NSString *subHaomiao =[haomiao substringToIndex:3];
    
    NSString *curentTime = [NSString stringWithFormat:@"%@%@",miao,subHaomiao];
//    NSLog(@"curentTime==========%@",curentTime);
    return curentTime;
}

// 比较时间
- (NSTimeInterval)compareTimeWithFormatTime:(NSString *)time
{
    // 获得当前时间
//    NSDate *ddd = [NSDate date];
//    NSLog(@"date==============%@",ddd);

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"Y-M-d H:m"];
    NSDate *d = [df dateFromString:time];
//    NSLog(@"%@",d);
    NSTimeInterval timeInterval = d.timeIntervalSinceNow;
    return timeInterval;
}

- (void)requestData
{

    NSString *currentTime = [self getTime];
    self.currentTime = currentTime;
    
    NSString *url = [NSString stringWithFormat:@"%@%@",kLiveUrl,currentTime];
    [LORequestManger GET:url success:^(id response) {
        NSMutableArray *superArray = (NSMutableArray *)response;
        self.liveModelArray = [NSMutableArray array];
        self.listArray = [NSMutableArray array];
        for (NSMutableDictionary *superDict in superArray) {
            LiveModel *totalLive = [LiveModel liveWithDict:superDict];

            [self.liveModelArray addObject:totalLive];
        }
        [self.liveView.leftTableView reloadData];
        [self.liveView.centerTableView reloadData];
        [self.liveView.rightTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"网络错误");
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self requestData];
    self.liveView = [[CommonTitleView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = _liveView;
    self.liveView.baseScrollView.delegate = self;
    self.liveView.leftTableView.delegate = self;
    self.liveView.leftTableView.dataSource = self;
    self.liveView.rightTableView.delegate = self;
    self.liveView.rightTableView.dataSource = self;
    
    self.liveView.leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.liveView.leftButton addTarget:self action:@selector(leftButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.liveView.rightButton addTarget:self action:@selector(rightButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.liveView.nextButton addTarget:self action:@selector(nextButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.liveView.recommendBtn setTitle:@"重要" forState: UIControlStateNormal];
    [self.liveView.collectionBtn setTitle:@"全部" forState: UIControlStateNormal];
    [self.liveView.videoBtn setTitle:@"关注" forState: UIControlStateNormal];
    
    [self.liveView.recommendBtn addTarget:self action:@selector(recommendBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.liveView.collectionBtn addTarget:self action:@selector(collectionBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.liveView.videoBtn addTarget:self action:@selector(videoBtnAction) forControlEvents:UIControlEventTouchUpInside];
    self.liveView.rightTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self setupLeftMenuButton];

    
    
    
    [self pullDownToRefresh];
    // 默认显示足球
    self.selected = NO;
    self.smallModel.focus = NO;
    self.importantSmallModel.focus = NO;

    [self cheakTheNetWorkStatus];
    
    NSMutableArray *foucsArray =  [LiveSqlite matchModel];
    
    for(ListArrayModel *foucsModel  in  foucsArray)
    {
        if([foucsModel.type isEqualToString:@"football"])
        {
            [self.footballFocusArray addObject:foucsModel];
        }else
        {
            [self.basketballFocusArray addObject:foucsModel];
        }
    }
    
}

-(void)cheakTheNetWorkStatus
{
    
    // 用法如下 必须调用此方法 才能监听到网络状态
    // 如果不调用 一直会是无网络状态 网络状态会一直判断错误
    [[NetWorkStateObserver defaultObserver]startNotifier];
    
    /*
     扩展Reachability.h 支持4G
     需要导入 coreTelephony.framewrok  和 Reachability
     在NetWorkStateObserver.m里 导入系统框架 #import <CoreTelephony/CTTelephonyNetworkInfo.h>
     */
    
    // 判断所对应网络的字符串
    NSString *networkState = [[NetWorkStateObserver defaultObserver]currentNetworkStatusDescription];
    // 无网络
    if ([networkState isEqualToString:@"network.status.none"])
    {
        [MBProgressHUD showError:@"没有网络,请稍后再试"];
        
        NSLog(@"没有任何网络");
    }
    // WIFI网络
    else if ([networkState isEqualToString:@"network.status.wifi"])
    {
        [MBProgressHUD showSuccess:@"你正在使用WIFI网络"];
        
        NSLog(@"WIFI网络");
    }else
    {
        [MBProgressHUD showMessage:@"你正在使用非WIFI网络"];
        
    }
    
    
    //    // 蜂窝移动网络
    //    else if ([networkState isEqualToString:@"network.status.wwan"])
    //    {
    //        NSLog(@"移动蜂窝网络");
    //    }
    //    // 2G网络
    //    else if ([networkState isEqualToString:@"network.status.2g"])
    //    {
    //      [MBProgressHUD showSuccess:@"你正在使用WIFI网络"];
    //
    //        NSLog(@"2G网络");
    //    }
    //    // 3G网络
    //    else if ([networkState isEqualToString:@"network.status.3g"])
    //    {
    //        NSLog(@"3G网络");
    //    }
    //    // 4G网络
    //    else if ([networkState isEqualToString:@"network.status.4g"])
    //    {
    //        NSLog(@"4G网络");
    //    }
    //    // 其他未知网络
    //    else
    //    {
    //        NSLog(@"未知网络");
    //    }
    
    
    // 判断是否有网络
    if ([[NetWorkStateObserver defaultObserver]currentNetworkStatus])
    {
        NSLog(@"有网络");
    }
    else
    {
        NSLog(@"无网络");
    }
    
}


#pragma mark - 下拉刷新
- (void)pullDownToRefresh
{
    __unsafe_unretained typeof(self) vc = self;
    self.liveView.leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.liveView.leftTableView addLegendHeaderWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 刷新表格
            [vc requestData];
            [vc.liveView.leftTableView reloadData];
            // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
            [vc.liveView.leftTableView.header endRefreshing];
        });
    }];
    self.liveView.centerTableView.delegate = self;
    self.liveView.centerTableView.dataSource = self;
    self.liveView.centerTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.liveView.centerTableView addLegendHeaderWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 刷新表格
            [vc requestData];
            [vc.liveView.centerTableView reloadData];
            // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
            [vc.liveView.centerTableView.header endRefreshing];
        });
        
    }];
}

#pragma mark - 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == self.liveView.rightTableView)
    {
        return 1;
    }
    else
    {
        return self.liveModelArray.count;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.liveView.leftTableView) {
        LiveModel *model = self.liveModelArray[section];
        self.listArray = ((!self.isSelected) ? model.importantFootball : model.importantBasketball);
    }
    else if (tableView == self.liveView.centerTableView)
    {
        LiveModel *model = self.liveModelArray[section];
        self.listArray = ((!self.isSelected) ? model.footballArray : model.basketballArray);
    }
    else
    {
        self.listArray = ((!self.isSelected) ? self.footballFocusArray : self.basketballFocusArray);
    }
    return _listArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
#pragma mark - 判断时间
    // 时间

    
#pragma mark - 中间
    if (tableView == self.liveView.centerTableView)
    {
        static NSString *cellID = @"cell";
        [tableView registerClass:[LiveTableViewCell class] forCellReuseIdentifier:cellID];
        
        LiveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        LiveModel *model = self.liveModelArray[indexPath.section];
        // 当前选中的是足球
        if (self.isSelected == NO)
        {
            self.smallModel = model.footballArray[indexPath.row];
            NSString *time = [NSString stringWithFormat:@"%@ %@",self.smallModel.sdate,self.smallModel.time];
            self.smallModel.timeInterval = [self compareTimeWithFormatTime:time];

            // 从数据库中取出model
            self.smallModel.focus = NO;
            for (ListArrayModel *footballModel in self.footballFocusArray) {
                if ([self.smallModel.title isEqualToString:footballModel.title]) {
                    self.smallModel.focus = YES;
                }
            }
        }
        // 当前选中的是篮球
        else
        {
            self.smallModel = model.basketballArray[indexPath.row];
            NSString *time = [NSString stringWithFormat:@"%@ %@",self.smallModel.sdate,self.smallModel.time];
            self.smallModel.timeInterval = [self compareTimeWithFormatTime:time];

            _smallModel.focus = NO;
            // 从数据库中取出model
            for (ListArrayModel *basketballModel in self.basketballFocusArray) {
                if ([self.smallModel.title isEqualToString:basketballModel.title]) {
                    self.smallModel.focus = YES;
                }
            }
        }
        
        // 判断时间：正在进行的比赛
        if (self.smallModel.timeInterval < 0)
        {
            [cell.attention setBackgroundImage:[UIImage imageNamed:@"LotteryHallLiveLogo@2x"] forState:UIControlStateNormal];
            cell.matchLabel.textColor = [UIColor redColor];
            cell.attention.userInteractionEnabled = NO;
        }
        else
        {
            cell.matchLabel.textColor = [UIColor blackColor];
            cell.attention.userInteractionEnabled = YES;

            // 关注比赛
            [cell.attention addTarget:self action:@selector(focusMatchWithButton2:) forControlEvents:UIControlEventTouchUpInside];
            if (self.smallModel.isFocused == YES)
            {
                [cell.attention setBackgroundImage:[UIImage imageNamed:@"football_collected_small_1"] forState:UIControlStateNormal];
            }
            else
            {
                [cell.attention setBackgroundImage:[UIImage imageNamed:@"football_collect_small_1"] forState:UIControlStateNormal];
            }
        }
        
        [cell setDataWithModel:self.smallModel];
        return cell;
    }
    
#pragma mark - 左边重要的比赛
    else if(tableView == self.liveView.leftTableView)
    {

        static NSString *cellID = @"cellID";
        [tableView registerClass:[LivePictureTableViewCell class] forCellReuseIdentifier:cellID];
        
        LivePictureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        LiveModel *model = self.liveModelArray[indexPath.section];
        
        if (self.isSelected == NO) {
            self.importantSmallModel = model.importantFootball[indexPath.row];
            NSString *time = [NSString stringWithFormat:@"%@ %@",self.importantSmallModel.sdate,self.importantSmallModel.time];
            self.importantSmallModel.timeInterval = [self compareTimeWithFormatTime:time];
            //            cell.matchLabel.font = [UIFont boldSystemFontOfSize:17];
            self.importantSmallModel.focus = NO;
            // 从数据库中取出model
            for (ListArrayModel *footballModel in self.footballFocusArray) {
                if ([self.importantSmallModel.title isEqualToString:footballModel.title]) {
                    self.importantSmallModel.focus = YES;
                }
            }
        }
        else
        {
            self.importantSmallModel = model.importantBasketball[indexPath.row];
            NSString *time = [NSString stringWithFormat:@"%@ %@",self.importantSmallModel.sdate,self.importantSmallModel.time];
            self.importantSmallModel.timeInterval = [self compareTimeWithFormatTime:time];
            self.importantSmallModel.focus = NO;
            // 从数据库中取出model
            for (ListArrayModel *basketballModel in self.basketballFocusArray) {
                if ([self.importantSmallModel.title isEqualToString:basketballModel.title]) {
                    self.importantSmallModel.focus = YES;
                }
            }
        }
        
        if (_importantSmallModel.timeInterval < 0) {
            [cell.attention setBackgroundImage:[UIImage imageNamed:@"LotteryHallLiveLogo@2x"] forState:UIControlStateNormal];
            cell.attention.userInteractionEnabled = NO;
        }
        else
        {
#pragma mark - 关注比赛
            // 给cell添加关注
            [cell.attention addTarget:self action:@selector(focusMatchWithButton:) forControlEvents:UIControlEventTouchUpInside];
            cell.attention.userInteractionEnabled = YES;
            if (self.importantSmallModel.isFocused) {
                [cell.attention setBackgroundImage:[UIImage imageNamed:@"football_collected_small"] forState:UIControlStateNormal];
            }
            else
            {
                [cell.attention setBackgroundImage:[UIImage imageNamed:@"football_collect_small"] forState:UIControlStateNormal];
            }
            //            cell.matchLabel.textColor = [UIColor blackColor];
        }
        [cell setDataWithModel:self.importantSmallModel];
        return cell;
    }
#pragma mark - 最右侧关注的比赛
    else
    {

        static NSString *cellID = @"cell";
        [tableView registerClass:[LiveTableViewCell class] forCellReuseIdentifier:cellID];
        
        LiveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [cell.attention addTarget:self action:@selector(focusMatchWithButton3:) forControlEvents:UIControlEventTouchUpInside];
        // 足球
        if (self.isSelected == NO) {
            ListArrayModel *smallModel = self.footballFocusArray[indexPath.row];
            NSString *time = [NSString stringWithFormat:@"%@ %@",smallModel.sdate,smallModel.time];
            self.smallModel.timeInterval = [self compareTimeWithFormatTime:time];
            if (smallModel.isFocused) {
                [cell.attention setBackgroundImage:[UIImage imageNamed:@"football_collected_small_1"] forState:UIControlStateNormal];
            }
            else
            {
                [cell.attention setBackgroundImage:[UIImage imageNamed:@"football_collect_small_1"] forState:UIControlStateNormal];
            }
            [cell setDataWithModel:smallModel];
            return cell;
        }
        
        // 篮球
        else
        {
            ListArrayModel *smallModel = self.basketballFocusArray[indexPath.row];
            NSString *time = [NSString stringWithFormat:@"%@ %@",smallModel.sdate,smallModel.time];
            self.smallModel.timeInterval = [self compareTimeWithFormatTime:time];
            if (smallModel.isFocused) {
                [cell.attention setBackgroundImage:[UIImage imageNamed:@"football_collected_small_1"] forState:UIControlStateNormal];
            }
            else
            {
                [cell.attention setBackgroundImage:[UIImage imageNamed:@"football_collect_small_1"] forState:UIControlStateNormal];
            }
            
            [cell setDataWithModel:smallModel];
        }
        
        // 判定时间
        if (_smallModel.timeInterval < 0) {
            cell.matchLabel.textColor = [UIColor redColor];
            
            cell.attention.userInteractionEnabled = NO;
        }
        else
        {
            cell.matchLabel.textColor = [UIColor blackColor];
            cell.attention.userInteractionEnabled = YES;
        }
        return cell;
    }
}

// 关注比赛  左边
- (void)focusMatchWithButton:(UIButton *)sender
{
    // 找到点击的cell
     LivePictureTableViewCell * cell = (LivePictureTableViewCell *)[[[sender superview] superview] superview];
    NSIndexPath * path = [self.liveView.leftTableView indexPathForCell:cell];
    //    NSLog(@"%@",path);
    LiveModel *model = self.liveModelArray[path.section];

    if (self.isSelected == NO)
    {
        ListArrayModel *smallModel = model.importantFootball[path.row];
        smallModel.focus = !smallModel.focus;
        if (smallModel.isFocused)
        {
            [self.footballFocusArray addObject:smallModel];
            [LiveSqlite addMatchWithModel:smallModel];
        }
        else
        {
            for(int i= 0; i< self.footballFocusArray.count;i++)
            {
                if([ [self.footballFocusArray[i] title] isEqualToString:smallModel.title])
                    
                {
                    [self.footballFocusArray removeObjectAtIndex:i];
                }
            }
            [LiveSqlite deleteMatchWithModel:smallModel];
        }
    }
    // 篮球
    else
    {
        ListArrayModel *smallModel = model.importantBasketball[path.row];
        smallModel.focus = !smallModel.focus;
        if (smallModel.isFocused) {
            [self.basketballFocusArray addObject:smallModel];
            [LiveSqlite addMatchWithModel:smallModel];
        }
        else
        {
            for(int i= 0; i< self.basketballFocusArray.count;i++)
            {
                if([ [self.basketballFocusArray[i] title] isEqualToString:smallModel.title])
                {
                    [self.basketballFocusArray removeObjectAtIndex:i];
                }
            }
            [LiveSqlite deleteMatchWithModel:smallModel];
        }
    }
    
    [self NowHasNoPoint];

    
    [self.liveView.leftTableView reloadData];
    [self.liveView.centerTableView reloadData];
    [self.liveView.rightTableView reloadData];
}
- (void)focusMatchWithButton2:(UIButton *)sender
{
    UITableViewCell * cell = (UITableViewCell *)[[sender superview] superview];
    NSIndexPath * path = [self.liveView.centerTableView indexPathForCell:cell];
    //    NSLog(@"%@",path);
    LiveModel *model = self.liveModelArray[path.section];
    if (self.isSelected == NO) {
        ListArrayModel *smallModel = model.footballArray[path.row];
        smallModel.focus = !smallModel.focus;
        if (smallModel.isFocused) {
            [self.footballFocusArray addObject:smallModel];
            [LiveSqlite addMatchWithModel:smallModel];
        }
        else
        {
            for(int i= 0; i< self.footballFocusArray.count;i++)
            {
                if([ [self.footballFocusArray[i] title] isEqualToString:smallModel.title])
                {
                    [self.footballFocusArray removeObjectAtIndex:i];
                }
            }
            [LiveSqlite deleteMatchWithModel:smallModel];
        }
    }
    // 篮球
    else
    {
        ListArrayModel *smallModel = model.basketballArray[path.row];
        smallModel.focus = !smallModel.focus;
        if (smallModel.isFocused) {
            [self.basketballFocusArray addObject:smallModel];
            [LiveSqlite addMatchWithModel:smallModel];
        }
        else
        {
            for(int i= 0; i< self.basketballFocusArray.count;i++)
            {
                if([ [self.basketballFocusArray[i] title] isEqualToString:smallModel.title])
                {
                    [self.basketballFocusArray removeObjectAtIndex:i];
                }
            }
            [LiveSqlite deleteMatchWithModel:smallModel];
        }
    }
    
    [self NowHasNoPoint];

    
    [self.liveView.leftTableView reloadData];
    [self.liveView.centerTableView reloadData];
    [self.liveView.rightTableView reloadData];
}
#pragma mark  关注按钮
- (void)focusMatchWithButton3:(UIButton *)sender
{
    UITableViewCell * cell = (UITableViewCell *)[[sender superview] superview];
    NSIndexPath * path = [self.liveView.rightTableView indexPathForCell:cell];
    //    NSLog(@"%@",path);
    if (self.isSelected == NO) {
        ListArrayModel *smallModel = self.footballFocusArray[path.row];
        smallModel.focus = !smallModel.focus;
        if (smallModel.isFocused) {
            [self.footballFocusArray addObject:smallModel];
            [LiveSqlite addMatchWithModel:smallModel];
        }
        else
        {
            for(int i= 0; i< self.footballFocusArray.count;i++)
            {
                if([ [self.footballFocusArray[i] title] isEqualToString:smallModel.title])
                {
                    [self.footballFocusArray removeObjectAtIndex:i];
                 }
             }
            [LiveSqlite deleteMatchWithModel:smallModel];
        }
    }
    else
    {
        ListArrayModel *smallModel = self.basketballFocusArray[path.row];
        smallModel.focus = !smallModel.focus;
        if (smallModel.isFocused) {
            [self.basketballFocusArray addObject:smallModel];
            [LiveSqlite addMatchWithModel:smallModel];
        }
        else
        {
            for(int i= 0; i< self.basketballFocusArray.count;i++)
            {
                if([ [self.basketballFocusArray[i] title] isEqualToString:smallModel.title])
                    
                {
                    [self.basketballFocusArray removeObjectAtIndex:i];
                    
                }
            }
            [LiveSqlite deleteMatchWithModel:smallModel];
        }
    }
    
    [self NowHasNoPoint];
    
    [self.liveView.leftTableView reloadData];
    [self.liveView.centerTableView reloadData];
    [self.liveView.rightTableView reloadData];
}

#pragma mark  暂无关注
-(void)NowHasNoPoint
{
    //没有关注的时候
    UIView *noDataView = [[UIView alloc]initWithFrame:CGRectMake(kScreenWidth*2, 0, kScreenWidth, kScreenHeight)];
    noDataView.backgroundColor = [UIColor whiteColor];
    
    UIImageView *pointPicture = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenWidth/2 - kScreenIp6(100), kScreenIp6(100), kScreenIp6(200), kScreenIp6(200))];
    pointPicture.image = [UIImage imageNamed:@"Empty_Hint_Fav@2x"];
    
    [noDataView addSubview:pointPicture];
    
    if(self.selected ==NO)
    {
        if(self.footballFocusArray.count == 0)
        {
            [self.liveView.baseScrollView  addSubview:noDataView];
        }
        else
        {
            [noDataView removeFromSuperview];
            [self.liveView.baseScrollView addSubview:self.liveView.rightTableView];
        }
    }
    else
    {
        if(self.basketballFocusArray.count == 0)
        {
            [self.liveView.baseScrollView  addSubview:noDataView];
        }
        else
        {
            [noDataView removeFromSuperview];
            [self.liveView.baseScrollView addSubview:self.liveView.rightTableView];
        }
    }
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.liveView.leftTableView) {
        return kScreenIp6(100);
    }
    else
    {
        return kScreenIp6(90);
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.liveView.rightButton.hidden)
    {
        if (tableView == self.liveView.centerTableView) {
            LiveTableViewController *liveTVC = [[LiveTableViewController alloc] init];
            LiveModel *model = self.liveModelArray[indexPath.section];
            if (self.isSelected == NO) {
                self.smallModel = model.footballArray[indexPath.row];
            }
            else
            {
                self.smallModel = model.basketballArray[indexPath.row];
            }
            NSArray *urlArray = [self.smallModel.url componentsSeparatedByString:@"."];
            liveTVC.urlLast = urlArray[0];
            liveTVC.currentTime = self.currentTime;
            liveTVC.model = self.smallModel;
            liveTVC.selected = self.isSelected;
            [self.navigationController pushViewController:liveTVC animated:YES];
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
        }
        else if (tableView == self.liveView.leftTableView)
        {
            LiveTableViewController *liveTVC = [[LiveTableViewController alloc] init];
            LiveModel *model = self.liveModelArray[indexPath.section];

            if (self.isSelected == NO) {
                self.smallModel = model.importantFootball[indexPath.row];
            }
            else
            {
                self.smallModel = model.importantBasketball[indexPath.row];
            }
            NSArray *urlArray = [self.smallModel.url componentsSeparatedByString:@"."];
            liveTVC.urlLast = urlArray[0];
            liveTVC.currentTime = self.currentTime;
            liveTVC.model = self.smallModel;
            liveTVC.selected = self.isSelected;

            [self.navigationController pushViewController:liveTVC animated:YES];
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
        }
        // 关注界面
        else
        {
            LiveTableViewController *liveTVC = [[LiveTableViewController alloc] init];

            if (self.isSelected == NO) {
                self.smallModel = self.footballFocusArray[indexPath.row];
            }
            else
            {
                self.smallModel = self.basketballFocusArray[indexPath.row];
            }
            NSArray *urlArray = [self.smallModel.url componentsSeparatedByString:@"."];
            liveTVC.urlLast = urlArray[0];
            liveTVC.currentTime = self.currentTime;
            liveTVC.model = self.smallModel;
            liveTVC.selected = self.isSelected;

            [self.navigationController pushViewController:liveTVC animated:YES];
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
        }
    }
    else
    {
        [self leftButton:self.liveView.leftButton];
    }
}

#pragma mark - 自定义分区表头
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    LiveModel *model = self.liveModelArray[section];
    if (self.isSelected == NO) {
        if (tableView == self.liveView.centerTableView) {
            // 足球
            
            if (model.footballArray.count == 0) {
                
                return 0;
            }
            else
            {
                
                return kScreenIp6(30);
            }
        }else if (tableView == self.liveView.leftTableView)
        {
            if (model.importantFootball.count == 0) {
                
                return 0;
            }
            else
            {
                
                return kScreenIp6(30);
            }
        }
        else
        {
            return kScreenIp6(30);
        }
    }
    
    else
    {
        if (tableView == self.liveView.centerTableView) {
            // 足球
            
            if (model.basketballArray.count == 0) {
                
                return 0;
            }
            else
            {
                
                return kScreenIp6(30);
            }
        }else if (tableView == self.liveView.leftTableView)
        {
            if (model.importantBasketball.count == 0) {
                
                return 0;
            }
            else
            {
                
                return kScreenIp6(30);
            }
        }
        else
        {
            return kScreenIp6(30);
        }
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (tableView == self.liveView.rightTableView) {
        SectionHeaderView *header = [[SectionHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenIp6(30))];
        header.hidden = YES;
        return header;
    }
    else
    {
        SectionHeaderView *header = [[SectionHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenIp6(30))];
        LiveModel *model = self.liveModelArray[section];
        header.headerImage.image = [UIImage imageNamed:@"data_title_green"];
        header.headerLabel.text = model.date;
        return header;
    }
}

#pragma mark - 隐藏和显示状态栏
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
}

#pragma mark - 点击导航栏右边按钮
- (void)rightButton:(UIButton *)sender
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    [UIView setAnimationDelegate:self];
    
    self.liveView.rightButton.hidden = YES;
    self.liveView.nextButton.hidden = NO;
    NSString *string = self.liveView.leftButton.titleLabel.text;
    [self.liveView.leftButton setTitle:self.liveView.rightButton.titleLabel.text forState:UIControlStateNormal];
    [self.liveView.rightButton setTitle:string forState:UIControlStateNormal];
    self.liveView.leftButton.frame = CGRectMake((kScreenWidth - kBtnWidth- kScreenIp6(10)) / 2, kTitleY, kBtnWidth, kBtnHeight);
    
    [UIView commitAnimations];
    self.selected = !self.selected;
    [self.liveView.leftTableView reloadData];
    [self.liveView.centerTableView reloadData];
    [self.liveView.rightTableView reloadData];

}

- (void)leftButton:(UIButton *)sender
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    [UIView setAnimationDelegate:self];
    
    if (self.liveView.rightButton.hidden)
    {
        self.liveView.rightButton.hidden = NO;
        self.liveView.nextButton.hidden = YES;
        self.liveView.leftButton.frame = CGRectMake(kScreenWidth / 2 - kBtnWidth - kScreenIp6(10), kTitleY, kBtnWidth, kBtnHeight);
    }
    else
    {
        self.liveView.rightButton.hidden = YES;
        self.liveView.nextButton.hidden = NO;
        self.liveView.leftButton.frame = CGRectMake((kScreenWidth - kBtnWidth- kScreenIp6(10)) / 2, kTitleY, kBtnWidth, kBtnHeight);
    }
    [UIView commitAnimations];
    
}

- (void)nextButton:(UIButton *)sender
{
    [self leftButton:self.liveView.leftButton];
}

#pragma mark - 点击屏幕默认选择左边按钮
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (!self.liveView.rightButton.hidden)
    {
        [self leftButton:self.liveView.leftButton];
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    
}

#pragma mark - 重要和全部和关注的点击事件
- (void)recommendBtnAction
{
    if (self.liveView.rightButton.hidden) {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationDelegate:self];
        
        
        self.liveView.baseScrollView.contentOffset = CGPointMake(0, 0);
        [self switchButtonPoint];
        
        
        [UIView commitAnimations];
    }
    else
    {
        [self leftButton:self.liveView.leftButton];
    }
}

- (void)collectionBtnAction
{
    if (self.liveView.rightButton.hidden) {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationDelegate:self];
        
        
        self.liveView.baseScrollView.contentOffset = CGPointMake(kScreenWidth, 0);
        [self switchButtonPoint];
        
        
        [UIView commitAnimations];
    }
    else
    {
        [self leftButton:self.liveView.leftButton];
    }
}

- (void)videoBtnAction
{
    if (self.liveView.rightButton.hidden) {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationDelegate:self];
        
        
        self.liveView.baseScrollView.contentOffset = CGPointMake(kScreenWidth*2, 0);
        [self switchButtonPoint];
        
        
        [UIView commitAnimations];
        
        [self NowHasNoPoint];

    }
    else
    {
        [self leftButton:self.liveView.leftButton];
    }
}

#pragma mark - 拖动中执行
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelegate:self];
    
    int i = (self.liveView.baseScrollView.contentOffset.x + kScreenWidth/2) / kScreenWidth;
    self.liveView.pointImage.center = CGPointMake((i * kScreenWidth/3 + kScreenWidth/6), kScreenIp6(106));
    
    [UIView commitAnimations];
}

#pragma mark 拖拽后减速停止触发
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationDelegate:self];
    
    [self switchButtonPoint];
    
    [UIView commitAnimations];
    
    if (scrollView.contentOffset.x == kScreenWidth *2)
    {
        
        [self NowHasNoPoint];
        
    }
}

// 切换 Button 指向
- (void)switchButtonPoint
{
    if (self.liveView.baseScrollView.contentOffset.x == 0)
    {
        self.liveView.pointImage.center = CGPointMake(kScreenWidth/6, kScreenIp6(106));
        [self.liveView.recommendBtn setTintColor:[UIColor whiteColor]];
        [self.liveView.collectionBtn setTintColor:[UIColor colorWithWhite:0.900 alpha:1.000]];
        [self.liveView.videoBtn setTintColor:[UIColor colorWithWhite:0.900 alpha:1.000]];
    }
    else if (self.liveView.baseScrollView.contentOffset.x == kScreenWidth)
    {
        self.liveView.pointImage.center = CGPointMake(kScreenWidth/2, kScreenIp6(106));
        [self.liveView.collectionBtn setTintColor:[UIColor whiteColor]];
        [self.liveView.recommendBtn setTintColor:[UIColor colorWithWhite:0.900 alpha:1.000]];
        [self.liveView.videoBtn setTintColor:[UIColor colorWithWhite:0.900 alpha:1.000]];
    }
    else
    {
        self.liveView.pointImage.center = CGPointMake(kScreenWidth*5/6, kScreenIp6(106));
        [self.liveView.videoBtn setTintColor:[UIColor whiteColor]];
        [self.liveView.collectionBtn setTintColor:[UIColor colorWithWhite:0.900 alpha:1.000]];
        [self.liveView.recommendBtn setTintColor:[UIColor colorWithWhite:0.900 alpha:1.000]];
    }
}

#pragma mark - 填出菜单按钮
-(void)setupLeftMenuButton{
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(kScreenIp6(10), kScreenIp6(20), kScreenIp6(40), kScreenIp6(40));
    [leftButton setBackgroundImage:[UIImage imageNamed:@"btn_menu_pressed_white@2x"] forState:UIControlStateNormal];
    [leftButton setTintColor:[UIColor colorWithWhite:0.900 alpha:1.000]];
    [leftButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.liveView addSubview:leftButton];
    

}

- (void)buttonAction:(UIButton *)button
{
    if (self.liveView.rightButton.hidden) {
        
        MineViewController *myVC = [[MineViewController alloc] init];
        //    [self presentViewController:myVC animated:YES completion:nil];
        [self.navigationController pushViewController:myVC animated:NO];
        self.tabBarController.tabBar.hidden = YES;
    }
    else
    {
        [self leftButton:self.liveView.leftButton];
    }
}


@end
